<?php

namespace IMT\EventBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use IMT\EventBundle\Form\EventGeneralType;
use IMT\EventBundle\Form\EventGeneralHandler;
use IMT\EventBundle\Form\EventAlertType;
use IMT\EventBundle\Form\EventAlertHandler;
use IMT\EventBundle\Entity\Event;
use IMT\EventBundle\Entity\Alert;
use IMT\EventBundle\Entity\ParticipantEvent;




class AddController extends Controller
{
 	public function loadAddEventAction($id)
                
    {
            $event = new event;
        return $this->render('IMTEventBundle:Add:addevent.html.twig',array( 
                       'event' => $event,'idAccount' => $id,
            ));
    }
    
    public function loadAddEventContentAction($id,$page)
    {
        
        
        
        
        $em = $this->getDoctrine()->getEntityManager();
        
       
         switch ($page){
             case "general" :
                 $event = new Event;
                 
                 $event->setCreaDate(new \Datetime());
                 $form = $this->createForm(new EventGeneralType, $event);
                 $event->setCreaDate(new \Datetime());
                 
                 $user = $this->container->get('security.context')->getToken()->getUser();
                 $event->setOwner($user->getId());
                 
                 
                 $formHandler = new EventGeneralHandler($form, $this->get('request'), $em);
                 

                
                if( $formHandler->process(false) )
                {
                  if ($id == 0){
                      $id = intval($this->container->getParameter('default_account_id'));
                  }
                  $participantEvent = new ParticipantEvent;
                  $participantEvent->setEvent($event);
                  $participantEvent->setAccount($em-> getRepository('IMTAccountBundle:Account')->find($id));
                  $participantEvent->setParticipant($user->getParticipant());
                  $participantEvent->setCreaDate(new \DateTime);
                  $participantEvent->setParticpationState(1);
                  
                  $event->addParticipantEvent($participantEvent);
                  $formHandler->onSuccess($event);
                   
                   return $this->redirect( $this->generateUrl('event_modif' , array('id' => $event->getId())) );
                   
                }
                
                
                
                    return $this->render('IMTEventBundle:Add:'.$page.'.html.twig', array(
                            'form' => $form->createView(),'id' => $event->getId(),'modif' => TRUE,'idAccount' => $id,
                   ));
                
                
                // case alerts
                case "alerts" :
                    
                 return $this->render('IMTEventBundle:Add:'.$page.'.html.twig', array(
                                'modif' => FALSE,
                    ));
                    
               //case file
               case "file" :
                    
                 return $this->render('IMTEventBundle:Add:'.$page.'.html.twig', array(
                                'modif' => FALSE,
                    ));
               
               //case privacy
               case "privacy" :
                    
                 return $this->render('IMTEventBundle:Add:'.$page.'.html.twig', array(
                                'modif' => FALSE,
                    ));    
                   
                   
                
         }
        

        
        
    //	$arrayView = array("general", "alerts", "file", "privacy");
    	
    //	if(in_array($page, $arrayView)){
    	//	return $this->render('IMTEventBundle:Add:'.$page.'.html.twig');
    	//}    	
    }
    
    
    public function loadModifEventAction($id)
    {
        $event = new Event;
        $event = $this->getDoctrine()->getEntityManager()-> getRepository('IMTEventBundle:Event')->find($id);
         return $this->render('IMTEventBundle:Add:addevent.html.twig',array( 
                       'event' => $event,
            ));
    }
    
    public function loadModifEventContentAction($id,$page)
    {
       
        
        
        
        $em = $this->getDoctrine()->getEntityManager();
        
       
         switch ($page){
             case "general" :
                 
                 $event = $em-> getRepository('IMTEventBundle:Event')->find($id);
                 $form = $this->createForm(new EventGeneralType, $event);
                 $formHandler = new EventGeneralHandler($form, $this->get('request'), $em);
                 $event->setModifDate(new \DateTime);
       

                if( $formHandler->process() )
                {
                   return $this->redirect( $this->generateUrl('event_modif' , array('id' => $event->getId())) );
                   
                }
                
                
                    return $this->render('IMTEventBundle:Add:'.$page.'.html.twig', array(
                            'form' => $form->createView(),'id' => $id,'modif' => TRUE,
                   ));
                
                
                // case alerts
                case "alerts" :
                 $alert = new Alert;
                 $form = $this->createForm(new EventAlertType, $alert);
                 $formHandler = new EventAlertHandler($form, $this->get('request'), $em, $id);

                // On exécute le traitement du formulaire. S'il retourne true, alors le formulaire a bien été traité
                if( $formHandler->process() )
                {
                    return $this->redirect( $this->generateUrl('accueil_planning') );
                }
                    
                    return $this->render('IMTEventBundle:Add:'.$page.'.html.twig', array(
                            'form' => $form->createView(),'id' => $id,'modif' => TRUE,
                   ));
                
                /*
                case "general" :
                 $event = new Event;
                 $form = $this->createForm(new EventGeneralType, $event);
                 $request = $this->get('request');
                 if( $request->getMethod() == 'POST' )
                 {
                    $form->bindRequest($request);
                    if( $form->isValid() )
                    {
                        $em = $this->getDoctrine()->getEntityManager();
                        $em->persist($event);
                        $em->flush();

                        return $this->redirect( $this->generateUrl('accueil_planning') );
                    }
                }else{
                    return $this->render('IMTEventBundle:Add:'.$page.'.html.twig', array(
                            'form' => $form->createView(),
                   ));
                }
                case "general" :
                 $event = new Event;
                 $form = $this->createForm(new EventGeneralType, $event);
                 $request = $this->get('request');
                 if( $request->getMethod() == 'POST' )
                 {
                    $form->bindRequest($request);
                    if( $form->isValid() )
                    {
                        $em = $this->getDoctrine()->getEntityManager();
                        $em->persist($event);
                        $em->flush();

                        return $this->redirect( $this->generateUrl('accueil_planning') );
                    }
                }else{
                    return $this->render('IMTEventBundle:Add:'.$page.'.html.twig', array(
                            'form' => $form->createView(),
                   ));
                }*/
         }
        

        
        
    //	$arrayView = array("general", "alerts", "file", "privacy");
    	
    //	if(in_array($page, $arrayView)){
    	//	return $this->render('IMTEventBundle:Add:'.$page.'.html.twig');
    	//}    	
    }
}


